package com.snail.readyvr.activity;

import android.app.Dialog;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.snail.readyvr.R;
import com.snail.readyvr.application.MyApplication;
import com.snail.readyvr.di.component.DaggerRegisterActivityPresenterComponent;
import com.snail.readyvr.di.module.RegisterActivityPresenterModule;
import com.snail.readyvr.event.LoginEvent;
import com.snail.readyvr.interfaces.MaxLengthWatcherListener;
import com.snail.readyvr.mvp.contract.RegisterActivityContract;
import com.snail.readyvr.mvp.model.EmailFindPswParam;
import com.snail.readyvr.mvp.model.EmailRegisterParam;
import com.snail.readyvr.mvp.model.GetEmailVerifyCodeParam;
import com.snail.readyvr.mvp.model.LoginParam;
import com.snail.readyvr.mvp.model.PhoneRegisterParam;
import com.snail.readyvr.mvp.presenter.RegisterActivityPresenter;
import com.snail.readyvr.rest.RestApiUtils;
import com.snail.readyvr.ui.ClearEditText;
import com.snail.readyvr.ui.IndicatorTiTle;
import com.snail.readyvr.ui.PhoneNumClearEditText;
import com.snail.readyvr.ui.ProgressButton;
import com.snail.readyvr.ui.ShowPassWordEditText;
import com.snail.readyvr.util.ApiException;
import com.snail.readyvr.util.AppUtils;
import com.snail.readyvr.util.Constant;
import com.snail.readyvr.util.DialogUtils;
import com.snail.readyvr.util.EventBus;
import com.snail.readyvr.util.IntentParams;
import com.snail.readyvr.util.MD5Utils;
import com.snail.readyvr.util.MaxLengthWatcher;
import com.snail.readyvr.util.StartActivityUtils;
import com.snail.readyvr.util.TBSnackBarUtils;
import com.snail.readyvr.util.ToastUtils;
import com.snail.readyvr.util.Utils;

import javax.inject.Inject;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.snail.readyvr.R.array.find_psw_title;

/**
 * Created by MRKING on 2016/9/27.
 */

public class RegisterActivity extends CheckPermissionsActivity implements MaxLengthWatcherListener,
        IndicatorTiTle.OnClickTitle, RegisterActivityContract.View {

    @BindArray(R.array.register_title)
    String[] mRegisterTitle;
    @BindArray(find_psw_title)
    String[] mFindPswTitle;
    @BindView(R.id.register_title_layout)
    RelativeLayout mTopTitleLayout;
    @BindView(R.id.register_back)
    FrameLayout mBack;
    @BindView(R.id.register_top_title)
    TextView mTopTitleText;
    @BindView(R.id.register_indicator)
    IndicatorTiTle mIndicatorTiTle;
    @BindView(R.id.register_content)
    LinearLayout mContentLayout;
    @BindView(R.id.error_tip)
    TextView mErrorTip;
    @BindView(R.id.register_btn)
    ProgressButton mProgressButton;
    @BindView(R.id.bing_tip)
    TextView mBingTip;
    @BindView(R.id.register_new_user_tip)
    TextView mRegisterNewUserTip;
    @BindView(R.id.bing_jump_tip)
    TextView mBindJump;
    @BindView(R.id.register_bottom_layout)
    RelativeLayout mRegisterBottom;

    private int from = Constant.FROM_REGISTER;
    private View mEmailRegisterView;
    private View mPhoneRegisterView;
    private View mEmailFindView;
    private ClearEditText mFindPswEmail;
    private boolean mFindPswEmailOK = false;
    private int mCurrPosition = 0;
    private ClearEditText mRegisterEmail;
    private boolean mRegisterEmailOK = false;
    private TextView mGetEmailVerificationCode;
    private ClearEditText mEmailVerificationCode;
    private boolean mEmailVerificationCodeOK = false;
    private ShowPassWordEditText mFindEmailPsw;
    private boolean mFindEmailPswOK = false;

    private ShowPassWordEditText mRegisterEmailPsw;
    private boolean mRegisterEmailPswOK = false;
    private PhoneNumClearEditText mPhoneNumClearEditText;
    private boolean mPhoneNumClearEditTextOK = false;
    private TextView mGetVerificationCode;
    private ClearEditText mVerificationCode;
    private boolean mVerificationCodeOK = false;
    private ShowPassWordEditText mRegisterPhonePsw;
    private boolean mRegisterPhonePswOK = false;
    private boolean isGettingVerificationCode = false;
    private boolean isGettingEmailVerificationCode = false;
    private boolean isRequestApi = false;


    @Inject
    RegisterActivityPresenter mPresenter;


    private CountDownTimer mCountDownTimer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            trickShow(millisUntilFinished);
        }

        @Override
        public void onFinish() {
            cancelTimer();
        }
    };

    private CountDownTimer mEmailCountDownTimer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            trickEmailShow(millisUntilFinished);
        }

        @Override
        public void onFinish() {
            cancelEmailTimer();
        }
    };
    private Dialog mDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        DaggerRegisterActivityPresenterComponent.builder()
                .applicationComponent(((MyApplication) getApplication())
                        .getApplicationComponent())
                .registerActivityPresenterModule(new RegisterActivityPresenterModule(this))
                .build().inject(this);
        from = getIntent().getIntExtra(IntentParams.REGISTER_FROM, Constant.FROM_REGISTER);
        mProgressButton.setEnabled(false);
        if (from == Constant.FROM_REGISTER || from == Constant.FROM_BIND || from == Constant.FROM_ME_BIND) {
            mEmailRegisterView = getLayoutInflater().inflate(R.layout.register_email_layout, null);
            mRegisterEmail = ButterKnife.findById(mEmailRegisterView, R.id.register_email);
            MaxLengthWatcher registerEmail = new MaxLengthWatcher(50, mRegisterEmail);
            registerEmail.setMaxLengthWatcherListener(this);
            mRegisterEmail.setTextWatcher(registerEmail);

            mRegisterEmailPsw = ButterKnife.findById(mEmailRegisterView, R.id.register_email_psw);

            MaxLengthWatcher registerEmailPsw = new MaxLengthWatcher(50, mRegisterEmailPsw);
            registerEmailPsw.setMaxLengthWatcherListener(this);
            mRegisterEmailPsw.setTextWatcher(registerEmailPsw);


        } else if (from == Constant.FROM_FIND_PSW) {
            mEmailFindView = getLayoutInflater().inflate(R.layout.find_psw_layout, null);
            mFindPswEmail = ButterKnife.findById(mEmailFindView, R.id.find_psw_username);
            MaxLengthWatcher findPswWatcher = new MaxLengthWatcher(50, mFindPswEmail);
            findPswWatcher.setMaxLengthWatcherListener(this);
            mFindPswEmail.setTextWatcher(findPswWatcher);

            mGetEmailVerificationCode = ButterKnife.findById(mEmailFindView, R.id.get_email_verification_code);
            mGetEmailVerificationCode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TBSnackBarUtils.showShort(RegisterActivity.this, R.string.get_email_verify_code_tip);
                    GetEmailVerifyCodeParam param = new GetEmailVerifyCodeParam();
                    param.setEmail(mFindPswEmail.getText().toString().trim());
                    param.setName("user");
                    mPresenter.getEmailVerifyCode(param);
                   //startEmailTimer();
                }
            });

            mEmailVerificationCode = ButterKnife.findById(mEmailFindView, R.id.email_verification_code);
            MaxLengthWatcher findPswEmailWatcher = new MaxLengthWatcher(50, mEmailVerificationCode);
            findPswEmailWatcher.setMaxLengthWatcherListener(this);
            mEmailVerificationCode.setTextWatcher(findPswEmailWatcher);

            mFindEmailPsw = ButterKnife.findById(mEmailFindView, R.id.find_email_psw);
            MaxLengthWatcher pswEmailWatcher = new MaxLengthWatcher(50, mFindEmailPsw);
            pswEmailWatcher.setMaxLengthWatcherListener(this);
            mFindEmailPsw.setTextWatcher(pswEmailWatcher);

        }
        mPhoneRegisterView = getLayoutInflater().inflate(R.layout.register_phonenum_layout, null);
        mPhoneNumClearEditText = ButterKnife.findById(mPhoneRegisterView, R.id.register_phone_num);
        MaxLengthWatcher textWatcher = new MaxLengthWatcher(13, mPhoneNumClearEditText);
        textWatcher.setMaxLengthWatcherListener(this);
        mPhoneNumClearEditText.setTextWatcher(textWatcher);

        mGetVerificationCode = ButterKnife.findById(mPhoneRegisterView, R.id.get_verification_code);
        mGetVerificationCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Utils.checkNetWork(RegisterActivity.this)) {
                    TBSnackBarUtils.showShort(RegisterActivity.this, R.string.get_verify_code_tip);
                    mPresenter.getPhoneVerifyCode(mPhoneNumClearEditText.getRealPhoneNum());
                }
            }
        });

        mVerificationCode = ButterKnife.findById(mPhoneRegisterView, R.id.register_verification_code);
        MaxLengthWatcher verificationCodeTextWatcher = new MaxLengthWatcher(50, mVerificationCode);
        verificationCodeTextWatcher.setMaxLengthWatcherListener(this);
        mVerificationCode.setTextWatcher(verificationCodeTextWatcher);

        mRegisterPhonePsw = ButterKnife.findById(mPhoneRegisterView, R.id.register_phone_num_psw);
        if (from == Constant.FROM_REGISTER || from == Constant.FROM_BIND || from == Constant.FROM_ME_BIND)
            mRegisterPhonePsw.setHint(R.string.input_psw_hint);
        else if (from == Constant.FROM_FIND_PSW)
            mRegisterPhonePsw.setHint(R.string.input_new_psw);
        MaxLengthWatcher registerPhonePswTextWatcher = new MaxLengthWatcher(50, mRegisterPhonePsw);
        registerPhonePswTextWatcher.setMaxLengthWatcherListener(this);
        mRegisterPhonePsw.setTextWatcher(registerPhonePswTextWatcher);


        switch (from) {
            case Constant.FROM_REGISTER:
                mTopTitleLayout.setVisibility(View.GONE);
                mIndicatorTiTle.setTitls(mRegisterTitle);
                mProgressButton.setText(R.string.register_btn_text);
                mBingTip.setVisibility(View.GONE);
                mRegisterNewUserTip.setVisibility(View.VISIBLE);
                mRegisterNewUserTip.setText(Utils.getUnderLineText(getString(R.string.register_new_user_tip)));
                mBindJump.setVisibility(View.GONE);
                mRegisterBottom.setVisibility(View.VISIBLE);


                mContentLayout.addView(mEmailRegisterView);
                break;
            case Constant.FROM_FIND_PSW:
                mTopTitleLayout.setVisibility(View.VISIBLE);
                mBack.setVisibility(View.VISIBLE);
                mTopTitleText.setText(R.string.find_psw_title);
                mIndicatorTiTle.setTitls(mFindPswTitle);
                mProgressButton.setText(R.string.find_psw_btn_text);
                mBingTip.setVisibility(View.GONE);
                mRegisterNewUserTip.setVisibility(View.GONE);
                mBindJump.setVisibility(View.GONE);
                mRegisterBottom.setVisibility(View.GONE);

                mContentLayout.addView(mEmailFindView);
                break;
            case Constant.FROM_BIND:
                caseBind();
                break;
            case Constant.FROM_ME_BIND:
                caseBind();
                break;
        }
        mIndicatorTiTle.setOnClickTitle(this);

    }

    private void caseBind() {
        mTopTitleLayout.setVisibility(View.VISIBLE);
        mBack.setVisibility(View.GONE);
        mTopTitleText.setText(R.string.bing_user_title);
        mIndicatorTiTle.setTitls(mRegisterTitle);
        mProgressButton.setText(R.string.bing_btn_text);
        mBingTip.setVisibility(View.VISIBLE);
        mRegisterNewUserTip.setVisibility(View.GONE);
        mBindJump.setVisibility(View.VISIBLE);
        mRegisterBottom.setVisibility(View.GONE);

        mContentLayout.addView(mEmailRegisterView);
    }


    @OnClick(R.id.go_on_login)
    public void onClickGotoLogin() {
        StartActivityUtils.startLoginActivity(RegisterActivity.this, null, LoginEvent.FROM_LOGIN);
        finish();
    }

    @OnClick(R.id.register_new_user_tip)
    public void onClickRegisterTip(){
        StartActivityUtils.startUserAgreementActivity(RegisterActivity.this);
    }

    @Override
    public void overMaxLength(View view, int max) {

    }

    @Override
    public void lengthChanged(View view, int length) {
        switch (view.getId()) {
            case R.id.find_psw_username:
                if (length > 0 && Utils.isEmail(mFindPswEmail.getText().toString())) {
                    mFindPswEmailOK = true;
                    if (!isGettingEmailVerificationCode)
                        mGetEmailVerificationCode.setEnabled(true);
                } else {
                    mFindPswEmailOK = false;
                    if (!isGettingEmailVerificationCode)
                        mGetEmailVerificationCode.setEnabled(false);
                }
                break;
            case R.id.email_verification_code:
                if (length > 0)
                    mEmailVerificationCodeOK = true;
                else
                    mEmailVerificationCodeOK = false;
                break;
            case R.id.find_email_psw:
                if (length > 0 && Utils.checkPsw(mFindEmailPsw.getText().toString().trim()))
                    mFindEmailPswOK = true;
                else
                    mFindEmailPswOK = false;
                break;
            case R.id.register_email:
                if (length > 0 && Utils.isEmail(mRegisterEmail.getText().toString()))
                    mRegisterEmailOK = true;
                else
                    mRegisterEmailOK = false;
                break;
            case R.id.register_email_psw:
                if (length > 0 && Utils.checkPsw(mRegisterEmailPsw.getText().toString().trim()))
                    mRegisterEmailPswOK = true;
                else
                    mRegisterEmailPswOK = false;
                break;
            case R.id.register_phone_num:

                if (length == 13) {
                    mPhoneNumClearEditTextOK = true;
                    if (!isGettingVerificationCode)
                        mGetVerificationCode.setEnabled(true);
                } else {
                    mPhoneNumClearEditTextOK = false;
                    if (!isGettingVerificationCode)
                        mGetVerificationCode.setEnabled(false);
                }
                break;
            case R.id.register_verification_code:
                if (length > 0)
                    mVerificationCodeOK = true;
                else
                    mVerificationCodeOK = false;
                break;
            case R.id.register_phone_num_psw:
                if (length > 0 && Utils.checkPsw(mRegisterPhonePsw.getText().toString().trim()))
                    mRegisterPhonePswOK = true;
                else
                    mRegisterPhonePswOK = false;
                break;
        }
        changeProgressState();
    }

    private void changeProgressState() {
        if(isRequestApi)
            return;
        switch (mCurrPosition) {
            case 0:
                if (from == Constant.FROM_REGISTER || from == Constant.FROM_BIND || from == Constant.FROM_ME_BIND) {
                    if (mRegisterEmailPswOK && mRegisterEmailOK) {
                        mProgressButton.setEnabled(true);
                    } else {
                        mProgressButton.setEnabled(false);
                    }
                } else if (from == Constant.FROM_FIND_PSW) {
                    if (mFindPswEmailOK && mEmailVerificationCodeOK && mFindEmailPswOK) {
                        mProgressButton.setEnabled(true);
                    } else {
                        mProgressButton.setEnabled(false);
                    }
                }
                break;
            case 1:
                if (mRegisterPhonePswOK && mVerificationCodeOK && mPhoneNumClearEditTextOK) {
                    mProgressButton.setEnabled(true);
                } else {
                    mProgressButton.setEnabled(false);
                }
                break;
        }
    }

    @Override
    public void onClickTitle(int position) {
        mCurrPosition = position;
        mContentLayout.removeAllViews();
        if (from == Constant.FROM_REGISTER || from == Constant.FROM_BIND || from == Constant.FROM_ME_BIND) {
            if (position == 0) {
                mContentLayout.addView(mEmailRegisterView);
            } else if (position == 1) {
                mContentLayout.addView(mPhoneRegisterView);
            }
        } else if (from == Constant.FROM_FIND_PSW) {
            if (position == 0) {
                mContentLayout.addView(mEmailFindView);
            } else if (position == 1) {
                mContentLayout.addView(mPhoneRegisterView);
            }
        }
        changeProgressState();
    }

    @OnClick(R.id.register_back)
    public void onClickBack() {
        finish();
    }

    @OnClick(R.id.bing_jump_tip)
    public void onJumpBing() {

    }

    private void startTimer() {
        mPhoneNumClearEditText.setEnabled(false);
        mVerificationCode.requestFocus();
        mGetVerificationCode.setEnabled(false);
        isGettingVerificationCode = true;
        mCountDownTimer.start();
        mGetVerificationCode.setTextSize(32);
        trickShow(60000);
    }

    private void cancelTimer() {
        isGettingVerificationCode = false;
        if (mPhoneNumClearEditTextOK)
            mGetVerificationCode.setEnabled(true);
        mCountDownTimer.cancel();
        finishShow();
    }

    private void trickShow(long time) {

        mGetVerificationCode.setText(time / 1000 + "");

    }

    private void finishShow() {
        mGetVerificationCode.setTextSize(14);
        mGetVerificationCode.setText(R.string.get_verification_code);
        changeProgressState();
    }

    private void startEmailTimer() {
        mGetEmailVerificationCode.setEnabled(false);
        isGettingEmailVerificationCode = true;
        mEmailVerificationCode.requestFocus();
        mEmailCountDownTimer.start();
        mGetEmailVerificationCode.setTextSize(32);
        trickEmailShow(60000);

    }

    private void cancelEmailTimer() {
        isGettingEmailVerificationCode = false;
        if (mFindPswEmailOK)
            mGetEmailVerificationCode.setEnabled(true);
        mEmailCountDownTimer.cancel();
        finishEmailShow();
    }

    private void trickEmailShow(long time) {

        mGetEmailVerificationCode.setText(time / 1000 + "");

    }

    private void finishEmailShow() {
        mGetEmailVerificationCode.setTextSize(14);
        mGetEmailVerificationCode.setText(R.string.get_verification_code);
        changeProgressState();
    }

    @OnClick(R.id.register_btn)
    public void onClickProgressBtn() {
        switch (from) {
            case Constant.FROM_REGISTER:
                //DialogUtils.showNewAccount(this,"skajsk@a.com");
                if (mCurrPosition == 0) {
                    if (Utils.checkNetWork(RegisterActivity.this)) {
                        changeEmailRegister();
                        DialogUtils.showEmialTip(this, getString(R.string.register_email_confirm), (getString(R.string.register_email_confirm_start) + mRegisterEmail.getText().toString().trim() + getString(R.string.register_email_confirm_end)),
                                mRegisterEmail.getText().toString().trim(), getString(R.string.ok), true, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mDialog = DialogUtils.showNewAccount(RegisterActivity.this, mRegisterEmail.getText().toString().trim());
                                        EmailRegisterParam param = new EmailRegisterParam();
                                        param.setType(Constant.REGISTER_PSW_TYPE);
                                        param.setPassword(MD5Utils.getMD5_16(mRegisterEmailPsw.getText().toString().trim()));
                                        param.setEmail(mRegisterEmail.getText().toString().trim());
                                        mPresenter.emailRegister(param);
                                    }

                                });
                    }
                }
                else if (mCurrPosition == 1) {
                    if (Utils.checkNetWork(RegisterActivity.this)) {
                        changePhoneRegister();
                        DialogUtils.showEmialTip(this, getString(R.string.register_phone_confirm), (getString(R.string.register_phone_confirm_start) + mPhoneNumClearEditText.getRealPhoneNum() + getString(R.string.register_phone_confirm_end)),
                                mPhoneNumClearEditText.getRealPhoneNum(), getString(R.string.ok), true, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mDialog = DialogUtils.showNewAccount(RegisterActivity.this, mPhoneNumClearEditText.getRealPhoneNum());
                                        PhoneRegisterParam phoneRegisterParam = new PhoneRegisterParam();
                                        phoneRegisterParam.setNumber(mPhoneNumClearEditText.getRealPhoneNum());
                                        phoneRegisterParam.setContent(mVerificationCode.getText().toString().trim());
                                        phoneRegisterParam.setPassword(MD5Utils.getMD5_16(mRegisterPhonePsw.getText().toString().trim()));
                                        phoneRegisterParam.setType(Constant.REGISTER_PSW_TYPE);

                                        mPresenter.phoneRegister(phoneRegisterParam);
                                    }

                                });

                    }
                }
                break;
            case Constant.FROM_FIND_PSW:
                if (mCurrPosition == 0) {
                    if (Utils.checkNetWork(RegisterActivity.this)) {
                        changeEmailFindPswState(true);
                        EmailFindPswParam param = new EmailFindPswParam();
                        param.setEmail(mFindPswEmail.getText().toString().trim());
                        param.setType(Constant.REGISTER_PSW_TYPE);
                        param.setPassword(MD5Utils.getMD5_16(mFindEmailPsw.getText().toString().trim()));
                        param.setContent(mEmailVerificationCode.getText().toString().trim());
                        mPresenter.emailFindPsw(param);
                    }

                } else if (mCurrPosition == 1) {
                    if (Utils.checkNetWork(RegisterActivity.this)) {
                        changePhoneFindPswState(true);
                        PhoneRegisterParam phoneRegisterParam = new PhoneRegisterParam();
                        phoneRegisterParam.setNumber(mPhoneNumClearEditText.getRealPhoneNum());
                        phoneRegisterParam.setContent(mVerificationCode.getText().toString().trim());
                        phoneRegisterParam.setPassword(MD5Utils.getMD5_16(mRegisterPhonePsw.getText().toString().trim()));
                        phoneRegisterParam.setType(Constant.REGISTER_PSW_TYPE);
                        mPresenter.phoneFindPsw(phoneRegisterParam);
                    }
                }
                break;
            case Constant.FROM_BIND:
                bind();
                break;
            case Constant.FROM_ME_BIND:
                bind();
                break;
        }
    }

    private void bind() {

    }

    private void changePhoneFindPswState(boolean requestApi){
        isRequestApi = requestApi;
        mVerificationCode.setActivated(false);
        mVerificationCode.setEnabled(!requestApi);
        mVerificationCode.clearFocus();
        mPhoneNumClearEditText.setActivated(false);
        mPhoneNumClearEditText.setEnabled(!requestApi);
        mPhoneNumClearEditText.clearFocus();
        mRegisterPhonePsw.setActivated(false);
        mRegisterPhonePsw.setEnabled(!requestApi);
        mRegisterPhonePsw.clearFocus();
        mIndicatorTiTle.setEnabled(!requestApi);
        mProgressButton.setShowProgress(requestApi);
        mProgressButton.setText(requestApi?R.string.submiting:R.string.find_psw_btn_text);
        mProgressButton.setEnabled(!requestApi);

    }

    private void changeEmailFindPswState(boolean requestApi){
        isRequestApi = requestApi;
        mEmailVerificationCode.setActivated(false);
        mEmailVerificationCode.setEnabled(!requestApi);
        mEmailVerificationCode.clearFocus();

        mFindPswEmail.setActivated(false);
        mFindPswEmail.setEnabled(!requestApi);
        mFindPswEmail.clearFocus();

        mFindEmailPsw.setActivated(false);
        mFindEmailPsw.setEnabled(!requestApi);
        mFindEmailPsw.clearFocus();

        mIndicatorTiTle.setEnabled(!requestApi);
        mProgressButton.setShowProgress(requestApi);
        mProgressButton.setText(requestApi?R.string.submiting:R.string.find_psw_btn_text);
        mProgressButton.setEnabled(!requestApi);

    }

    @Override
    public void getPhoneVerifyCodeSuccess() {
        TBSnackBarUtils.showShort(RegisterActivity.this, R.string.get_verify_code_ok);
        startTimer();
    }

    @Override
    public void phoneRegisterSuccess() {
        ToastUtils.show(RegisterActivity.this, R.string.register_ok);
        LoginParam param = new LoginParam();
        param.setType(Constant.REGISTER_PSW_TYPE);
        param.setPassword(MD5Utils.getMD5_16(mRegisterPhonePsw.getText().toString().trim()));
        param.setDevid(AppUtils.getWIFIMac(MyApplication.getmContext()));
        param.setUsername(mPhoneNumClearEditText.getRealPhoneNum());
        if(mPresenter != null)
            mPresenter.autoLogin(LoginEvent.FROM_NORMAL_LOGIN,param);
    }

    @Override
    public void phoneFindPswSuccess() {

        ToastUtils.show(this,R.string.find_psw_success_tip);
        if(mCurrPosition == 0)
            StartActivityUtils.startLoginActivity(this,mFindPswEmail.getText().toString().trim(),LoginEvent.FROM_LOGIN);
        else if(mCurrPosition == 1)
            StartActivityUtils.startLoginActivity(this,mPhoneNumClearEditText.getRealPhoneNum(),LoginEvent.FROM_LOGIN);
        finish();

    }

    @Override
    public void emailRegisterSuccess() {
        ToastUtils.show(RegisterActivity.this, R.string.register_ok);

        LoginParam param = new LoginParam();
        param.setType(Constant.REGISTER_PSW_TYPE);
        param.setPassword(MD5Utils.getMD5_16(mRegisterEmailPsw.getText().toString().trim()));
        param.setDevid(AppUtils.getWIFIMac(MyApplication.getmContext()));
        param.setUsername(mRegisterEmail.getText().toString().trim());
        if(mPresenter != null)
            mPresenter.autoLogin(LoginEvent.FROM_NORMAL_LOGIN,param);
    }

    @Override
    public void getEmailVerifyCodeSuccess() {
        TBSnackBarUtils.showShort(RegisterActivity.this, R.string.get_email_verify_code_ok);
        startEmailTimer();

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void dismissProgress() {

    }

    @Override
    public void error(Throwable throwable) {
        if(mDialog != null)
            mDialog.dismiss();
        if(from == Constant.FROM_FIND_PSW && mCurrPosition == 1)
            changePhoneFindPswState(false);
         else if(from == Constant.FROM_FIND_PSW && mCurrPosition == 0)
            changeEmailFindPswState(false);
        if (throwable != null) {
            if (throwable instanceof ApiException) {
                ApiException apiException = (ApiException) throwable;
                TBSnackBarUtils.showShort(this, Utils.getErrorTip(apiException.getCode()) + "");
                if(apiException.getWhichApi() == RestApiUtils.PHONE_REGISTER_API){
                    if(apiException.getCode() == 3006 || apiException.getCode() == 3007 ){
                        mPhoneNumClearEditText.setActivated(true);
                    }
                }else if(apiException.getWhichApi() == RestApiUtils.EMAIL_REGISTER_API){
                    if(apiException.getCode() == 3006 || apiException.getCode() == 3007 ){
                        mRegisterEmail.setActivated(true);
                    }
                }else if(apiException.getWhichApi() == RestApiUtils.PHONE_FIND_PSW_API){//手机找回密码错误特殊提示

                }
            } else {
                TBSnackBarUtils.showShort(this, throwable.getMessage() + "");
            }
        }else {
            TBSnackBarUtils.showShort(this, R.string.code_default);
        }
    }



    public void changePhoneRegister() {

        mVerificationCode.setActivated(false);
        mVerificationCode.clearFocus();
        mPhoneNumClearEditText.setActivated(false);
        mPhoneNumClearEditText.clearFocus();
        mRegisterPhonePsw.setActivated(false);
        mRegisterPhonePsw.clearFocus();

    }
    public void changeEmailRegister() {

        mRegisterEmail.setActivated(false);
        mRegisterEmail.clearFocus();
        mRegisterEmailPsw.setActivated(false);
        mRegisterEmailPsw.clearFocus();

    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag(Constant.BusTag.LOGIN_TAG)})
    public void onLoginFinish(LoginEvent event){
        if (event != null) {
            if (event.getType() == LoginEvent.TYPE_FAILE) {
                loginfailed();
            } else if (event.getType() == LoginEvent.TYPE_SUCCESS && event.getFrom() == LoginEvent.FROM_NORMAL_LOGIN) {
                ToastUtils.show(this, R.string.auto_login_ok);
                StartActivityUtils.startVideoPlayActivity(this);
                //StartActivityUtils.startSelectChannelActivity(this);
            }
        } else {
            loginfailed();
        }
        if(mDialog != null)
            mDialog.dismiss();
        finish();
    }

    private void loginfailed() {
        ToastUtils.show(this, R.string.auto_login_failed);
        StartActivityUtils.startLoginActivity(this,mPhoneNumClearEditText.getRealPhoneNum(), LoginEvent.FROM_LOGIN);
    }

    @Override
    protected void hasGetAllPermissions() {

    }
}
